package com.wzt.base.controller;

import com.wzt.base.service.IFastDfsService;
import com.wzt.base.service.impl.FastDfsServiceImpl;
import com.wzt.base.util.AjaxResponse;
import com.wzt.base.util.FastdfsUtil;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * @author Zenith
 */
@RestController
@RequestMapping("/fastDfs")
public class FastDFS {
    @Autowired
    private IFastDfsService fastDfsService;

    @PostMapping
    public AjaxResponse upload(MultipartFile file) {
        try {
            byte[] bytes = file.getBytes();
            // 文件原名
            String originalFilename = file.getOriginalFilename();
            // 文件扩展名
            String extension = FilenameUtils.getExtension(originalFilename);
            // 调用文件上传
            String filePath = fastDfsService.uploadFile(bytes, extension);
            return AjaxResponse.oneResponse().setData(filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("上传失败！" + e.toString());
        }
    }

    @DeleteMapping
    public AjaxResponse delete(String path) {
        try {
            System.out.println(path);
            return AjaxResponse.oneResponse();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResponse.oneResponse().setSuccess(false).setMessage("上传失败！" + e.toString());
        }
    }
}
